package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.edu.bean.Carinfo;
import com.edu.bean.Orders;
import com.edu.dao.BaseDao;
import com.edu.dao.CarinfoDao;

public class CarinfoDaoImpl implements CarinfoDao {

	@Override
	public List<Carinfo> getcarinfoall(int pageNum, int pageSize) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Carinfo> carinfo = new ArrayList();
		String sql = "SELECT * FROM car_info ORDER BY carId DESC LIMIT ?,?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, (pageNum-1)*pageSize);
			ps.setInt(2, pageSize);
			res = ps.executeQuery();
			while(res.next()) {
				Carinfo cf = new Carinfo();
				cf.setCarId(res.getInt("carId"));
				cf.setCarName(res.getString("carName"));
				cf.setPrice(res.getDouble("price"));
				cf.setColor(res.getString("color"));
				cf.setInterior_accessories(res.getString("interior_accessories"));
				cf.setConfiguration(res.getString("configuration"));
				cf.setCarImg(res.getString("carImg"));
				cf.setIntroduce(res.getString("introduce"));
				cf.setPerformance(res.getString("performance"));
				cf.setGuarantee(res.getString("guarantee"));
				cf.setInterior_material(res.getString("interior_material"));
				cf.setSmart_device(res.getString("smart_device"));
				carinfo.add(cf);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return carinfo;
	}

	@Override
	public int selectAllcarinfo(Carinfo carinfo) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int result = 0;
		String sql = "SELECT count(1) as total FROM car_info";
		List<Object> obj = new ArrayList<Object>();
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				result = res.getInt("total");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return result;
	}

	@Override
	public int delectcarinfo(int carinfoid) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "DELETE FROM car_info WHERE carId = ?";
		List<Object> obj = new ArrayList<Object>();
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, carinfoid);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		
		return result;
	}

	@Override
	public Carinfo selectcarinfoid(int carinfoid) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Carinfo carinfo=null;
		String sql = "SELECT * FROM car_info WHERE carId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1,carinfoid);
			res = ps.executeQuery();
			if(res.next()) {
				carinfo = new Carinfo();
				carinfo.setCarId(res.getInt("carId"));
				carinfo.setCarName(res.getString("carName"));
				carinfo.setPrice(res.getDouble("price"));
				carinfo.setColor(res.getString("color"));
				carinfo.setInterior_accessories(res.getString("interior_accessories"));
				carinfo.setConfiguration(res.getString("configuration"));
				carinfo.setCarImg(res.getString("carImg"));
				carinfo.setIntroduce(res.getString("introduce"));
				carinfo.setPerformance(res.getString("performance"));
				carinfo.setGuarantee(res.getString("guarantee"));
				carinfo.setInterior_material(res.getString("interior_material"));
				carinfo.setSmart_device(res.getString("smart_device"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return carinfo;
	}

	@Override
	public int updatecarinfo(Carinfo carinfo) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE car_info set "
				+ "carName = ?,price = ?,color= ?,Interior_material= ?,configuration= ?,introduce= ?,performance= ?,guarantee= ?,interior_accessories= ?,smart_device= ?";
				if(carinfo.getCarImg()!=null) {
					sql +=",carImg = ?";
				}
				sql+= " WHERE carId = ? ";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, carinfo.getCarName());
			ps.setDouble(2, carinfo.getPrice());
			ps.setString(3, carinfo.getColor());
			ps.setString(4,carinfo.getInterior_material());
			ps.setString(5,carinfo.getConfiguration());
			ps.setString(6,carinfo.getIntroduce());
			ps.setString(7,carinfo.getPerformance());
			ps.setString(8,carinfo.getGuarantee());
			ps.setString(9,carinfo.getInterior_accessories());
			ps.setString(10,carinfo.getSmart_device());
			if(carinfo.getCarImg()!=null) {
				ps.setString(11,carinfo.getCarImg());
				ps.setInt(12, carinfo.getCarId());
			}else {
				ps.setInt(11, carinfo.getCarId());
			}
			result = ps.executeUpdate();
			System.out.println(ps);
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public int addcarinfo(Carinfo carinfo) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql="";
		if(carinfo.getCarImg() == null) {
			sql = "INSERT INTO car_info(carName,price,color,Interior_material,configuration,introduce,performance,guarantee,interior_accessories,smart_device) VALUES (?,?,?,?,?,?,?,?,?,?)";
		}else {
			sql = "INSERT INTO car_info(carName,price,color,Interior_material,configuration,introduce,performance,guarantee,interior_accessories,smart_device,carImg) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
		}
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, carinfo.getCarName());
			ps.setDouble(2, carinfo.getPrice());
			ps.setString(3, carinfo.getColor());
			ps.setString(4,carinfo.getInterior_material());
			ps.setString(5,carinfo.getConfiguration());
			ps.setString(6,carinfo.getIntroduce());
			ps.setString(7,carinfo.getPerformance());
			ps.setString(8,carinfo.getGuarantee());
			ps.setString(9,carinfo.getInterior_accessories());
			ps.setString(10,carinfo.getSmart_device());
			if(carinfo.getCarImg()!=null) {
				ps.setString(11,carinfo.getCarImg());
			}
			result = ps.executeUpdate();
			System.out.println(ps);
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

}
